package com.matt.service.impl;

import com.matt.domain.ChildrenLeaveRequest;
import com.matt.domain.ChildrenVaccine;
import com.matt.repository.ChildrenLeaveRequestRepository;
import com.matt.repository.ChildrenVaccineRepository;
import com.matt.service.ChildrenLeaveRequestService;
import com.matt.service.ChildrenVaccineService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * *******************************
 * Created by Ron.
 * form com.matt
 * on 2017/7/13
 * *******************************
 */
@Service
@Slf4j
public class ChildrenVaccineServiceImpl implements ChildrenVaccineService {

    @Autowired
    private ChildrenVaccineRepository childrenVaccineRepository;


    @Override
    public void addChildrenLeaveRequests(List<ChildrenVaccine> childrenVaccines) {
        childrenVaccineRepository.saveAll(childrenVaccines);
    }

    @Override
    public void addChildrenLeaveRequest(ChildrenVaccine childrenVaccine) {
        childrenVaccineRepository.save(childrenVaccine);
    }

    @Override
    public Page<ChildrenVaccine> selChildrenLeaveRequest(String aid,String classesId, String chldrenId, String type, Integer state, String name, Integer pageNum, Integer pageSize) {
        // 使用 Specification 构建动态查询条件
        Specification<ChildrenVaccine> specification = (root, query, criteriaBuilder) -> {
            // 创建一个空的 Predicate 列表，用于存储查询条件
            List<Predicate> predicates = new ArrayList<>();

            // 判断 classesId 是否为空，不为空则添加到查询条件中
            if (StringUtils.isNotEmpty(classesId)) {
                predicates.add(criteriaBuilder.equal(root.get("classesId"), classesId));
            }

            if (chldrenId != null && !chldrenId.isEmpty()) {
                predicates.add(criteriaBuilder.equal(root.get("childrenId"), chldrenId));
            }

            if (StringUtils.isNotEmpty(aid)) {
                predicates.add(criteriaBuilder.equal(root.get("aid"), aid));
            }

            if (type != null && !type.isEmpty()) {
                predicates.add(criteriaBuilder.equal(root.get("type"), type));
            }

            if (state != null) {
                predicates.add(criteriaBuilder.equal(root.get("state"), state));
            }

            // 判断 name 是否为空，不为空则添加到查询条件中
            if (name != null && !name.isEmpty()) {
                predicates.add(criteriaBuilder.like(root.get("name"), "%" + name + "%"));
            }

            // 将多个条件用 AND 连接
            return criteriaBuilder.and(predicates.toArray(new Predicate[0]));
        };

        Sort sort = Sort.by(Sort.Order.desc("createTime"));

        Page<ChildrenVaccine> all = childrenVaccineRepository.findAll(specification, PageRequest.of(pageNum, pageSize,sort));
        return all;
    }

    @Override
    public List<ChildrenVaccine> selChildrenLeaveRequestList(String classesId,String childrenId,String type, Integer state) {

        // 使用 Specification 构建动态查询条件
        Specification<ChildrenVaccine> specification = (root, query, criteriaBuilder) -> {
            // 创建一个空的 Predicate 列表，用于存储查询条件
            List<Predicate> predicates = new ArrayList<>();

            // 判断 classesId 是否为空，不为空则添加到查询条件中
            if (classesId != null && !classesId.isEmpty()) {
                predicates.add(criteriaBuilder.equal(root.get("classesId"), classesId));
            }

            if (type != null && !type.isEmpty()) {
                predicates.add(criteriaBuilder.equal(root.get("type"), type));
            }

            if (state != null) {
                predicates.add(criteriaBuilder.equal(root.get("state"), state));
            }

            if (childrenId != null && !childrenId.isEmpty()) {
                predicates.add(criteriaBuilder.equal(root.get("childrenId"), childrenId));
            }

            // 将多个条件用 AND 连接
            return criteriaBuilder.and(predicates.toArray(new Predicate[0]));
        };

        List<ChildrenVaccine> all = childrenVaccineRepository.findAll(specification);
        return all;
    }


    @Override
    public ChildrenVaccine selChildrenVaccineById(String id) {
        return childrenVaccineRepository.findById(id).get();
    }

    @Override
    public ChildrenVaccine selChildrenVaccineByChildrenId(String childrenId,String type) {

        // 使用 Specification 构建动态查询条件
        Specification<ChildrenVaccine> specification = (root, query, criteriaBuilder) -> {

        // 创建一个空的 Predicate 列表，用于存储查询条件
        List<Predicate> predicates = new ArrayList<>();

        // 判断 classesId 是否为空，不为空则添加到查询条件中
        if (childrenId != null && !childrenId.isEmpty()) {
            predicates.add(criteriaBuilder.equal(root.get("childrenId"), childrenId));
        }

        if (type != null) {
            predicates.add(criteriaBuilder.equal(root.get("type"), type));
        }
            // 将多个条件用 AND 连接
            return criteriaBuilder.and(predicates.toArray(new Predicate[0]));
        };

        Optional<ChildrenVaccine> one = childrenVaccineRepository.findOne(specification);
        if (one.isPresent()){
            ChildrenVaccine all = childrenVaccineRepository.findOne(specification).get();
            return all;
        }
        return null;
    }


    @Override
    public void delChildrenVaccines(String id) {
        childrenVaccineRepository.deleteById(id);
    }


}
